package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.CodeUtil;
import com.wing.order.dao.DistributionDemandPlanDao;
import com.wing.order.dao.DynamicDao;
import com.wing.order.model.entity.DistributionDemandPlan;
import com.wing.order.model.request.DistributionDemandPlanForm;
import com.wing.order.model.response.DistributionDemandPlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 配货需求计划表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DistributionDemandPlanServiceImpl extends ServiceImpl<DistributionDemandPlanDao, DistributionDemandPlan> implements DistributionDemandPlanService {
    @Autowired
    DynamicDao dynamicDao;
    @Override
    public int add(DistributionDemandPlanForm distributionDemandPlanForm) {

        //校验
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String today = format.format(new Date());
        DistributionDemandPlan distributionDemandPlan =new DistributionDemandPlan();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DistributionDemandPlan.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(distributionDemandPlanForm, distributionDemandPlan, copyOptions);
        Integer maxCode = dynamicDao.getDayMaxNo("ord_distribution_demand_plan");
        String code = CodeUtil.getCode(maxCode);
        distributionDemandPlan.setDisbCode("D" + code);
        distributionDemandPlan.setDay(Integer.valueOf(today));
        return baseMapper.insert(distributionDemandPlan);
    }

    @Override
    public List<DistributionDemandPlanVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DistributionDemandPlanVO> pageList4app(Page page, Map<String, Object> params) {
        List<DistributionDemandPlanVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DistributionDemandPlanVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
